﻿@using Partner.Surround.Web.Startup
@using Partner.Surround.Authorization
@{
    ViewBag.CurrentPageName = PageNames.Roles;
}

<!--角色列表-->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-btn-container">
                @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Frame_Roles_Delete))
                {
                    <button class="layui-btn layui-btn-role" data-type="batchdel">删除</button>
                }
                @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Frame_Roles_Create))
                {
                    <button class="layui-btn layui-btn-role" data-type="add">添加</button>
                }
            </div>
            <div class="layui-row">
                <div class="layui-col-xs12">
                    <table class="layui-table"
                           lay-data="{height: 'full-120', page:true, id:'roleList'}"
                           lay-filter="roleList">
                        <thead>
                            <tr>
                                <th lay-data="{checkbox:true, fixed: true,width:40}"></th>
                                <th lay-data="{field:'name'}">角色名称</th>
                                <th lay-data="{field:'displayName', sort: true}">对外名称</th>
                                @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Frame_Roles_Update) ||
                               await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Frame_Roles_Delete))
                                {
                                    <th lay-data="{fixed:'right', align:'center', toolbar: '#roleBarList'}"></th>
                                }
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!--列表按钮-->
<script type="text/html" id="roleBarList">
    <div class="layui-table-cell laytable-cell-1-0-9">
        @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Frame_Roles_Update))
        {
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        }
        @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Frame_Roles_Delete))
        {
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        }
    </div>
</script>

@section scripts{
    <script type="text/javascript">
        layui.config({
            base: '/lib/layui-extend-modules/' //静态资源所在路径
        }).use(['jquery', 'layer', 'table'], function () {
            var $ = layui.$,
                table = layui.table;

            //主列表加载，可反复调用进行刷新
            var config = {};  //table的参数，如搜索key，点击tree的id
            var loadRoleList = function (options) {
                if (options != undefined) {
                    $.extend(config, options);
                }
                table.reload('roleList', {
                    url: "@Url.Action("GetRoleList", "Roles")",
                    where: config,
                    parseData: function (res) {
                        return {
                            "code": res.result.code, //解析接口状态
                            "msg": res.result.msg, //解析提示文本
                            "count": res.result.count, //解析数据长度
                            "data": res.result.data //解析数据列表
                        };
                    },
                    response: {
                        statusCode: 200 //规定成功的状态码，默认：0
                    }
                });
            };
            loadRoleList();

            //删除角色
            function deleteRole(data) {
                parent.layer.confirm('确定删除吗?', function (index) {
                    abp.ajax({
                        url: "@Url.Action("DeleteRole", "Roles")",
                        data: JSON.stringify(data),
                        abpHandleError: false
                    }).done(function (data) {
                        if (data.code == 200) {
                            abp.notify.success(data.msg);
                            loadRoleList();
                        }
                    }).fail(function (jqXHR) {
                        parent.layer.msg(jqXHR.message, { icon: 5 });
                    }).always(function () {
                        parent.layer.close(index);
                    }); 
                });
            }

            //创建或更新角色
            function createOrUpdateRole(roleData) {
                parent.layer.open({
                    type: 2,
                    title: roleData.title,
                    content: roleData.content,
                    area: ['500px','426px'],
                    btn: ['确定', '取消'],
                    yes: function(index, layero){
                        parent.window['layui-layer-iframe'+ index].layui.submitForm();
                    }
                });
            }

            //创建或更新完毕回调
            layui.callback = function (data) {
                parent.layer.close(data.index); //关闭弹层
                abp.notify.success(data.msg);
                loadRoleList();
            }

            //监听工具条
            table.on('tool(roleList)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {   //删除
                    deleteRole([{ "id": data.id }]);
                } else if (obj.event === 'edit') {  //编辑
                    var roleData = {
                        title: '编辑角色信息',
                        content: "@Url.Action("CreateOrUpdateRole", "Roles")"+"?roleId=" + data.id,
                    };
                    createOrUpdateRole(roleData);
                }
            });

            //监听页面主按钮操作
            var active = {
                add: function () {//添加
                    var roleData = {
                        title: '添加新角色',
                        content: "@Url.Action("CreateOrUpdateRole", "Roles")",
                    };
                    createOrUpdateRole(roleData);
                },
                batchdel: function () { //批量删除
                    var checkStatus = table.checkStatus('roleList'),
                        checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return parent.layer.msg('请选择数据');
                    }

                    var ids = checkData.map(function (d) { return { "id": d.id }; });
                    deleteRole(ids);
                }
            };

            //点击事件转发器
            $('.layui-btn.layui-btn-role').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
}

